<?php
class User extends CI_Controller{
	public function register(){
		if (!$this->input->post()) {
			$this->load->helper(array('form'));
			$template_data['title'] = 'Iniciar sesi&oacute;n';
            $template_data['content'] = $this->load->view('user/register', array(), True);
            $this->load->view('templates/main', $template_data);
			return;
		}
	
		$username = $this->input->post('username');
		$password = hash('sha256',$this->input->post('password'));
		$email = $this->input->post('email');
		$dni = $this->input->post('dni');
		$ingressFile = $this->input->post('archivoCuota');
		$federativeType = $this->input->post('federativeType');
		$ingFedFile = $this->input->post('archivoFede');
		
		$sql_error = "SELECT * FROM users WHERE username=".$this->db->escape($username);
		$query = $this->db->query($sql_error);
		if($query->num_rows()>0){
			$this->load->helper(array('form'));
			$template_data['title'] = 'Iniciar sesi&oacute;n';
            $template_data['content'] = $this->load->view('user/register', array('error' => "Ya existe el username"), True);
            $this->load->view('templates/main', $template_data);
			return ;
		}
		
		$sql = "INSERT INTO users (username, password,email,dni,archivoCuota,federado,archivoFede) 
        VALUES (".$this->db->escape($username).", ".$this->db->escape($password).",".$this->db->escape($email).",".$this->db->escape($dni).",".$this->db->escape($ingressFile).",".$this->db->escape($federativeType).",".$this->db->escape($ingFedFile).")";

		$this->db->query($sql);
		$template_data['title'] = 'Iniciar sesi&oacute;n';
        $template_data['content'] = $this->load->view('user/validate', array(), True);
        $this->load->view('templates/main', $template_data);

	}

	public function profile(){
		$this->load->helper(array('form'));
		$this->load->library('form_validation');
		$session_data = $this->session->userdata('logged_in');
		$data = $session_data['username'];
		$this->load->model('userModel');
		$usuario = $this->userModel->perfilUsuario($data);
    	$template_data['title'] = 'Perfil';
        $template_data['content'] = $this->load->view('user/profile', $usuario, True);
        $this->load->view('templates/main', $template_data);
	}

	public function list_users(){
		$this->load->model('userModel');
		$usuarios['sql'] = $this->userModel->obtenerUsuarios();
		$template_data['title'] = 'Lista de Usuarios';
		$template_data['content'] = $this->load->view('user/list', $usuarios, True);
        $this->load->view('templates/main', $template_data);
	}
	
	public function actualizar(){
		
		$oldPass = hash('sha256',$this->input->post('password'));
		$session_data = $this->session->userdata('logged_in');
		$data = $session_data['username'];
		$this->load->model('userModel');
		$usuario = $this->userModel->perfilUsuario($data);
		
		if($oldPass == $usuario->password){
			$password = hash('sha256',$this->input->post('newPassword'));
			$this->db->set('password', $password);
		}
		$username = $this->input->post('username');
		$email = $this->input->post('email');
		$name = $this->input->post('name');
		$last_name = $this->input->post('last_name');
		$dni = $this->input->post('dni');
		$address = $this->input->post('address');
		$zip = $this->input->post('zip');
		$town = $this->input->post('town');
		$province = $this->input->post('province');
		$sex = $this->input->post('sex');
		$phone = $this->input->post('phone');
		$birth_date = $this->input->post('birth_date');
		
		$this->db->set('email', $email);
		$this->db->set('name', $name);
		$this->db->set('last_name', $last_name);
		$this->db->set('dni', $dni);
		$this->db->set('address', $address);
		$this->db->set('zip', $zip);
		$this->db->set('town', $town);
		$this->db->set('province', $province);
		$this->db->set('sex', $sex);
		$this->db->set('phone', $phone);
		$this->db->set('birth_date', $birth_date);
		
		$this->db->where('username', $username);
		$this->db->update('users');
		
		redirect("user/profile","refresh");
	}
}